package com.george.shiro.model.service.impl;

import com.george.shiro.holder.RequestHolder;
import com.george.shiro.model.entity.User;
import com.george.shiro.model.dao.UserMapper;
import com.george.shiro.model.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.george.shiro.util.IpUtil;
import com.george.shiro.vo.UserConditionVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author george
 * @since 2019-05-21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public List<User> findUserList() {
        List<User> userList = this.baseMapper.selectUserList();
        return userList;
    }

    @Override
    public User findByUsername(String username) {
        User user = this.baseMapper.selectByUsername(username);
        return user;
    }

    @Override
    public void updateUserLastLoginInfo(User user) {
        user.setLastLoginTime(new Date());
        user.setLastLoginIp(IpUtil.getRealIp(RequestHolder.getRequest()));
        user.setPassword(null);
        this.baseMapper.updateById(user);
    }

    @Override
    public PageInfo<User> findPageBreakByCondition(UserConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<User> userList = this.baseMapper.findPageBreakByCondition(vo);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        pageInfo.setList(userList);
        return pageInfo;
    }

    @Override
    public List<User> listByRoleId(Long roleId) {
        List<User> userList = this.baseMapper.listByRoleId(roleId);
        return userList;
    }
}
